//
//  TiXianVC.m
//  QoilPublic
//
//  Created by SunChao on 17/9/18.
//  Copyright © 2017年 杨凯. All rights reserved.
//


#import "BingdingVC.h"
#import "BindingCardVC.h"
#import "BindingWXVC.h"
#import "BindingZFBVC.h"

#define btnWidth ([UIScreen mainScreen].bounds.size.width)/3
@interface BingdingVC ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIScrollView *bScrollView;

@property(nonatomic,strong)UIView *btitleView;

@property(nonatomic,strong)UIView *selectView;

@end

@implementation BingdingVC{
    
    NSArray *_titleArr;
    NSMutableArray *_btnArr;
    //为了移除通知*********
    BindingCardVC *_vc2;//银行卡
    BindingWXVC *_vc3;//微信
    BindingZFBVC *_vc4;//支付宝
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"绑定";
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _titleArr = @[@"银行卡",@"微信",@"支付宝"];
    
    _btnArr = [NSMutableArray array];
    [self createUI];
    [self addtitleButton];
    [self addController];
    BindingCardVC *vc = (BindingCardVC *)[self.childViewControllers firstObject];
    
    vc.view.frame = self.bScrollView.bounds;
    [self.bScrollView addSubview:vc.view];
    self.view.clipsToBounds = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollToFinishedOrder) name:kNotificationGoToPay object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollToReceivedOrder) name:@"orderOrder" object:nil];
}

- (void)createUI{
    WS(wSelf);
    
    [self.view addSubview:self.btitleView];
    [_btitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wSelf.view.mas_top).offset(0);
        make.left.equalTo(wSelf.view.mas_left).offset(0);
        make.right.equalTo(wSelf.view.mas_right).offset(0);
        make.height.equalTo(@45);
    }];
    self.selectView.frame = CGRectMake(0, 43, btnWidth, 2);
    [self.btitleView addSubview:self.selectView];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [QColorUtility defaultLineColor];
    [self.btitleView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wSelf.view.mas_left).offset(0);
        make.right.equalTo(wSelf.view.mas_right).offset(0);
        make.height.equalTo(@0.5);
        make.bottom.equalTo(wSelf.btitleView.mas_bottom).offset(0);
    }];
    
    [self.view addSubview:self.bScrollView];
    [_bScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_btitleView.mas_bottom).offset(0);
        make.left.equalTo(wSelf.view.mas_left).offset(0);
        make.right.equalTo(wSelf.view.mas_right).offset(0);
        make.bottom.equalTo(wSelf.view.mas_bottom).offset(0);
    }];
    self.bScrollView.contentSize = CGSizeMake(_titleArr.count*kScreenWidth, 0);
}


#pragma mark --- 添加银行卡/微信/支付宝
- (void)addtitleButton{
    for (int i=0; i<_titleArr.count; ++i) {
        UIButton *button = [QViewUtility createButtonWithFrame:CGRectMake(i*btnWidth,0, btnWidth,43) font:Ksize15 title:_titleArr[i] titleColor:[QColorUtility defaultBackColor] backgroundColor:[UIColor whiteColor]];
        button.tag = i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        if(button.tag == 0){
            [button setTitleColor:[QColorUtility defaultBlueColor] forState:UIControlStateNormal];
        }else{
            [button setTitleColor:[QColorUtility defaultBackColor] forState:UIControlStateNormal];
        }
        [self.btitleView addSubview:button];
        [_btnArr addObject:button];
    }
}


#pragma mark --- 添加控制器
- (void)addController{
    
    BindingCardVC *vc2 = [[BindingCardVC alloc] init];
    _vc2 = vc2;
    [self addChildViewController:vc2];
    
    BindingWXVC *vc3 = [[BindingWXVC alloc] init];
    _vc3 = vc3;
    [self addChildViewController:vc3];
    
    BindingZFBVC *vc4 = [[BindingZFBVC alloc] init];
    _vc4 = vc4;
    [self addChildViewController:vc4];
}


#pragma mark ---点击微信
- (void)scrollToReceivedOrder{
    
    UIButton *btn = _btnArr[0];
    [self buttonAction:btn];
}

#pragma mark ---点击支付宝
- (void)scrollToFinishedOrder{
    
    UIButton *btn = _btnArr[2];
    [self buttonAction:btn];
}

//#pragma mark ---点击投诉方法
//- (void)callPhoneAction{
//    
//    [QViewUtility showPlayBoxView:PlayboxCallPhoneStyle myBlock:^(NSInteger index) {
//        if(index == 1){
//            [QViewUtility callPhone];
//        }
//    }];
//}

#pragma mark --- scrollView位置偏移
- (void)buttonAction:(UIButton *)btn{
    [_vc2.view endEditing:YES];
    [_vc3.view endEditing:YES];
    [_vc4.view endEditing:YES];
    CGFloat offsetX = btn.tag * self.bScrollView.frame.size.width;
    CGFloat offsetY = self.bScrollView.contentOffset.y;
    CGPoint offset = CGPointMake(offsetX, offsetY);
    [self.bScrollView setContentOffset:offset animated:YES];
}

#pragma mark --- scrollviewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    int index = scrollView.contentOffset.x/self.bScrollView.frame.size.width;
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationscrollsToTop object:nil userInfo:@{kTopKey:[NSString stringWithFormat:@"%d",index]}];
    UIViewController *vc = self.childViewControllers[index];
    [self setupButtonColor:index];
    if(vc.view.superview){
        return;
    }else{
        vc.view.frame = scrollView.bounds;
        [self.bScrollView addSubview:vc.view];
    }
}

//手势调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSUInteger index = scrollView.contentOffset.x/self.bScrollView.frame.size.width;
    [UIView animateWithDuration:0.23 animations:^{
        _selectView.frame = CGRectMake(index*btnWidth, 43, btnWidth, 2);
    }];
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

//滑动调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [_vc2.pickview remove];
    [_vc2.view endEditing:YES];
    [_vc3.view endEditing:YES];
    [_vc4.view endEditing:YES];
    CGFloat index = (scrollView.contentOffset.x/self.bScrollView.frame.size.width);
    _selectView.frame = CGRectMake(btnWidth * index , 43, btnWidth, 2);
    CGFloat scrollViewLeftX = scrollView.contentOffset.x;
    if(scrollViewLeftX < 0){
        self.bScrollView.scrollEnabled = NO;
    }else{
        self.bScrollView.scrollEnabled = YES;
    }
    
}

//设置button颜色
- (void)setupButtonColor:(NSInteger)index{
    
    for (UIButton *btn in _btnArr) {
        if(btn.tag == index){
            [btn setTitleColor:[QColorUtility defaultBlueColor] forState:UIControlStateNormal];
        }else{
            [btn setTitleColor:[QColorUtility defaultBackColor] forState:UIControlStateNormal];
        }
    }
}

#pragma mark --- get/set
- (UIScrollView *)bScrollView{
    if (!_bScrollView) {
        _bScrollView = [[UIScrollView alloc] init];
        _bScrollView.showsHorizontalScrollIndicator = NO;
        _bScrollView.showsVerticalScrollIndicator = NO;
        _bScrollView.pagingEnabled = YES;
        _bScrollView.delegate = self;
        _bScrollView.scrollsToTop = NO;
    }
    return _bScrollView;
}

- (UIView *)btitleView{
    if (!_btitleView) {
        _btitleView = [[UIView alloc] init];
        _btitleView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.7];
    }
    return _btitleView;
}

- (UIView *)selectView{
    if (!_selectView) {
        _selectView = [[UIView alloc] init];
        _selectView.backgroundColor = [QColorUtility defaultBlueColor];
    }
    return _selectView;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:_vc2];
    [[NSNotificationCenter defaultCenter] removeObserver:_vc3];
    [[NSNotificationCenter defaultCenter] removeObserver:_vc4];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

